
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ImagePreview extends JPanel implements MouseListener, ActionListener, ChangeListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	static Color pauseRemoveColor = new Color(.5f, .4f, .5f, 1.0f);
	static Color pauseRemoveColorHover = new Color(30, 140, 220, 100);
		
	boolean enabled = false; 
	boolean isBig = true;
	
	JCheckBox progressPanel;
	
	JLabel imageBox;
	JPanel imageCont = new JPanel();
	
	JLabel left; 	
	JLabel right;
	
	ImageIcon leftImg;
	ImageIcon rightImg;
	
	Dimension panelSize;
	
	Color defaultBack;
	
	EditedImage image;
	
	GridBagConstraints constr= new GridBagConstraints();
	
	ImagePreview(Dimension d, EditedImage img, boolean isBig) {
		super();
		
		this.loadIcons();
		setControllButtons();
		
		imageCont.setLayout(null);
		imageCont.setPreferredSize(new Dimension(128,128));
		this.image = img;
		panelSize = d;
		this.defaultBack = this.getBackground();
		
		
		//this.setPreferredSize(d);
		
		this.addMouseListener(this);
		
		this.setLayout(new GridBagLayout());
		
		
		setImagePreview();
		
		setProgressPanel();
		
		this.setEnabled(true);
		
		
		this.setImage(isBig, img);		
		this.setBorder(BorderFactory.createLineBorder(this.defaultBack, 3));
		}

	
	private void loadIcons() {		
		try {
			BufferedImage leftBuf = ImageIO.read(ImageUploader.class.getResourceAsStream("left.png"));			
			leftImg = new ImageIcon(leftBuf);
			
			BufferedImage rightBuf = ImageIO.read(ImageUploader.class.getResourceAsStream("right.png"));
			rightImg = new ImageIcon(rightBuf);			
			}
		
		catch (IOException e) {			
			e.printStackTrace();
			}
		
		}
	public void setProgress(float progress) {
		
		}

	public void reSetImage() {
		this.setImage(this.isBig, this.image);
		}
	
	public void setImage(boolean isBig, EditedImage image) {
		this.isBig = isBig;
		this.image = image;
		if (this.image == null)
			return;
		if (this.isBig)
			imageBox.setIcon(this.image.thumbnailImageBig);
			
		else
			imageBox.setIcon(this.image.thumbnailImageBig);
		
		this.progressPanel.setSelected(image.upload);
		
		this.left.repaint();
		this.right.repaint();		
		this.left.setVisible(false);
		this.right.setVisible(false);
		}
		

	private void setImagePreview() {		
		imageBox = new JLabel();
		
		imageBox.setBounds(0,0,128, 128);
		imageCont.add(imageBox);
		//imageBox.setPreferredSize(new Dimension(128, 128));		
		this.constr.gridx = 0;
		this.constr.gridy = 0;
		this.add(imageCont, this.constr);
	}

	JTextField x;
	JTextField y;
	
	private void setProgressPanel() {
		
//		JPanel progressPanel = new JPanel();
//		progressPanel.setLayout(new FlowLayout());
//		
//		JLabel progressLabel = new JLabel("Progress:");		
//		progressPanel.add(progressLabel, this.constr);
//		
//		
//		ProgressBar progressBar = new ProgressBar(new Dimension(40, 10));
//		progressPanel.add(progressBar, this.constr);
		
		progressPanel = new JCheckBox("Uploadaj");		
		progressPanel.addChangeListener(this);
		this.constr.gridx = 0;
		this.constr.gridy = 1;		
		this.add(progressPanel, this.constr);		
		
//		x = new JTextField("400");
//		y = new JTextField("400");
//		this.constr.gridx = 0;
//		this.constr.gridy = 2;
//		this.add(x, this.constr);
//		this.constr.gridx = 0;
//		this.constr.gridy = 3;
//		this.add(y, this.constr);
	}

	private void setControllButtons() {
//		JPanel controllPanel = new JPanel();
//		controllPanel.setLayout(new FlowLayout());
		
		left = new JLabel();
		left.setIcon(this.leftImg);
		left.setPreferredSize(new Dimension(24,24));
		left.setBounds(128-60, 128-30, 24, 24);
		left.addMouseListener(this);
		left.setVisible(false);
		this.imageCont.add(left);
			
			
		
		right = new JLabel();		
		right.setIcon(this.rightImg);		
		right.setBounds(128-30, 128-30, 24, 24);
		right.addMouseListener(this);
		right.setVisible(false);
		this.imageCont.add(right);
					
		//this.add(right);
		
		
	}


	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		if (arg0.getSource() == left) {
			this.left.setVisible(true);
			this.right.setVisible(true);
			left.setBackground(ImagePreview.pauseRemoveColorHover);
			left.setOpaque(true);
			}
		else if (arg0.getSource() == right) {
			this.left.setVisible(true);
			this.right.setVisible(true);
			right.setBackground(pauseRemoveColorHover);
			right.setOpaque(true);
			}
		else {
			if (this.enabled) {
				//this.setBackground(new Color(.5f,.5f,.5f,1.0f));
				this.left.setVisible(true);
				this.right.setVisible(true);
				}
			
		}
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		if (arg0.getSource() == left) {
			left.setBackground(null);
			left.setOpaque(false);
			
			}
		else if (arg0.getSource() == right) {
			right.setBackground(null);
			right.setOpaque(false);
			}
		else {
			if (this.enabled) {
				
				//this.setBackground(this.defaultBack);
				this.left.setVisible(false);
				this.right.setVisible(false);
				}
		}
	}
	
	public void setEnabled(boolean enabled) {
		this.progressPanel.setEnabled(enabled);
		left.setVisible(enabled);
		right.setVisible(enabled);
		this.enabled = enabled;
		}

	@Override
	public void mousePressed(MouseEvent arg0) {
		if (arg0.getSource() == this.right) {
//			image.x = Integer.parseInt(x.getText());
//			image.y = Integer.parseInt(y.getText());
			image.rotateLeft(); 
			this.reSetImage();
			}
		else if (arg0.getSource() == this.left) {
//			image.x = Integer.parseInt(x.getText());
//			image.y = Integer.parseInt(y.getText());
			image.rotateRight();
			this.reSetImage();
			}		
		}	

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

		}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		JOptionPane.showMessageDialog(null, "Remove");		
		
	}

	@Override
	public void stateChanged(ChangeEvent arg0) {
		if ((this.image != null)&&(this.progressPanel != null))
			this.image.upload = this.progressPanel.isSelected();
		
	}
	
	
}
